package com.mystudio.android.calendar.model;

import com.mystudio.android.calendar.R;
import com.mystudio.android.calendar.constants.Constants;
import com.mystudio.android.calendar.util.CPSyncUtil;
import com.mystudio.android.calendar.util.LunarCalendar;
import com.mystudio.android.util.Logger;

import android.content.Context;
import android.content.res.Resources;
import android.text.format.Time;

public class CalendarConfig {
	private static final Logger logger = new Logger(Constants.LOG_TAG, CalendarConfig.class);
	private static float pixelScale = -1; // Used for supporting different screen densities
	
	private boolean initialized = false;
	
	/**
	 * Start day of a week, Time.SATURDAY or Time.SUNDAY
	 */
	public int weekStartDay = Time.SUNDAY;
	
	/**
	 * Number of weeks to show on one page
	 */
	public int noOfWeeks = 5;
	
	/**
	 * Configurable layout parameters in density independent pixels
	 */
    public final int weekGap = 0;
    public final int monthDayGap = 1;
    public final int monthDayTextSize = 14;
    public final int monthDayTextTopMargin = 0;
    public final int monthDayTextLeftMargin = 3;
    public final int monthDayTextBottomMargin = 0;
    public final int eventTextSize = 10;
    public final int eventTextTopMargin = 0;
    public final int eventTextLeftMargin = 2;
    public final int eventTextRightMargin = 2;
    public final int chiDayTextSize = 9;
    public final int chiDaytextTopMargin = 2;
    public final int chiDayTextRightMargin = 3;
	
	/**
	 * Calculated layout parameters in real pixels
	 */
    public int WEEK_GAP = 0;
    public int MONTH_DAY_GAP = 1;
    public int MONTH_DAY_TEXT_SIZE = 14;
    public int MONTH_DAY_TEXT_TOP_MARGIN = 0;
    public int MONTH_DAY_TEXT_LEFT_MARGIN = 3;
    public int MONTH_DAY_TEXT_BOTTOM_MARGIN = 0;
    public int EVENT_TEXT_SIZE = 10;
    public int EVENT_TEXT_TOP_MARGIN = 0;
    public int EVENT_TEXT_LEFT_MARGIN = 2;
    public int EVENT_TEXT_RIGHT_MARGIN = 2;
    public int CHI_DAY_TEXT_SIZE = 9;
    public int CHI_DAY_TEXT_TOP_MARGIN = 2;
    public int CHI_DAY_TEXT_RIGHT_MARGIN = 3;

    public int cellWidth;
    public int cellHeight;
    
    // Cached colors
    public int mMonthBorderColor;
    public int mMonthBgColor;
    public int mMonthDayNumberColor;
    public int mMonthChiDayNumberColor;
    public int mMonthChiSDayNumberColor;
    public int mMonthOtherMonthBgColor;
    public int mMonthOtherMonthDayNumberColor;
    public int mMonthTodayBgColor;
    public int mMonthTodayNumberColor;
    public int mMonthTodayChiNumberColor;
    public int mMonthSundayBgColor;
    public int mMonthSundayNumberColor;
    public int mMonthSaturdayBgColor;
    public int mMonthSaturdayNumberColor;
    public int mMonthDayNotCurrMthColor;
    public int mMonthDaySelectedColor;

	/**
	 * Cache objects for reuse
	 */
    public LunarCalendar lunarCalendar;
    public Time today;
    	
	public void initialize(Context context) {
		if (pixelScale < 0) {
			pixelScale = context.getResources().getDisplayMetrics().density;
		}
		
		if (!initialized) {
			if (pixelScale != 1) {
				WEEK_GAP = (int)(weekGap * pixelScale);
				MONTH_DAY_GAP = (int)(monthDayGap * pixelScale);
				MONTH_DAY_TEXT_SIZE = (int)(monthDayTextSize * pixelScale);
			    MONTH_DAY_TEXT_TOP_MARGIN = (int)(monthDayTextTopMargin * pixelScale);
			    MONTH_DAY_TEXT_LEFT_MARGIN = (int)(monthDayTextLeftMargin * pixelScale);
			    MONTH_DAY_TEXT_BOTTOM_MARGIN = (int)(monthDayTextBottomMargin * pixelScale);
			    EVENT_TEXT_SIZE = (int)(eventTextSize * pixelScale);
			    EVENT_TEXT_TOP_MARGIN = (int)(eventTextTopMargin * pixelScale);
			    EVENT_TEXT_LEFT_MARGIN = (int)(eventTextLeftMargin * pixelScale);
			    EVENT_TEXT_RIGHT_MARGIN = (int)(eventTextRightMargin * pixelScale);
			    CHI_DAY_TEXT_SIZE = (int)(chiDayTextSize * pixelScale);
			    CHI_DAY_TEXT_TOP_MARGIN = (int)(chiDaytextTopMargin * pixelScale);
			    CHI_DAY_TEXT_RIGHT_MARGIN = (int)(chiDayTextRightMargin * pixelScale);
			}
			initialized = true;
		}
		
        // Cache color lookups
        Resources res = context.getResources();
        mMonthBorderColor = res.getColor(R.color.month_border);
        mMonthBgColor = res.getColor(R.color.month_bg);
        mMonthDayNumberColor = res.getColor(R.color.month_day_number);
        mMonthChiDayNumberColor = res.getColor(R.color.month_chi_day_number);
        mMonthChiSDayNumberColor = res.getColor(R.color.month_chi_sday_number);
        mMonthOtherMonthBgColor = res.getColor(R.color.month_other_month_bg);
        mMonthOtherMonthDayNumberColor = res.getColor(R.color.month_other_month_day_number);
        mMonthTodayBgColor = res.getColor(R.color.month_today_bg);
        mMonthTodayNumberColor = res.getColor(R.color.month_today_day_number);
        mMonthTodayChiNumberColor = res.getColor(R.color.month_today_chi_day_number);
        mMonthSundayBgColor = res.getColor(R.color.month_sun_bg);
        mMonthSundayNumberColor = res.getColor(R.color.month_sun_day_number);
        mMonthSaturdayBgColor = res.getColor(R.color.month_sat_bg);
        mMonthSaturdayNumberColor = res.getColor(R.color.month_sat_day_number);
        mMonthDayNotCurrMthColor = res.getColor(R.color.month_day_not_curr_mth);
        mMonthDaySelectedColor = res.getColor(R.color.month_day_selected);
        
        today = new Time();
        today.set(System.currentTimeMillis());
        
        lunarCalendar = new LunarCalendar();
	}
	
	public void changeCellDimension(int areaWidth, int areaHeight) {
		cellWidth = (areaWidth - (6 * MONTH_DAY_GAP)) / 7;
		cellHeight = (areaHeight - (noOfWeeks * WEEK_GAP)) / noOfWeeks;
	}
}
